/**
 * @author antonio081014
 * @since Dec 13, 2011, 10:37:47 PM
 */

public class DucksAlignment {
	public int minimumTime(String[] grid) {
		return Math.min(minCol(grid), minCol(transform(grid)));
	}

	public String[] transform(String[] grid) {
		int N = grid.length;
		int M = grid[0].length();
		String[] ret = new String[M];
		for (int i = 0; i < M; i++) {
			String tmp = "";
			for (int j = 0; j < N; j++)
				tmp += grid[j].charAt(i);
			ret[i] = tmp;
		}
		return ret;
	}

	public int minCol(String[] grid) {
		int N = grid.length;
		int M = grid[0].length();
		int ret = Integer.MAX_VALUE;
		for (int i = 0; i < M; i++) {
			int tmp = 0;
			boolean flag = false;
			for (int j = 0; j < N; j++) {
				int index = grid[j].indexOf('o');
				if (index > -1) {
					tmp += Math.abs(i - index);
					flag = true;
				} else if (flag) {
					tmp = Integer.MAX_VALUE;
					break;
				}
			}
			if (!flag)
				tmp = Integer.MAX_VALUE;
			ret = Math.min(ret, tmp);
		}
		return ret;
	}

	 public static void main(String[] args)
	{
		long time;
		int answer;
		boolean errors = false;
		int desiredAnswer;
		
		
		time = System.currentTimeMillis();
		answer = new DucksAlignment().minimumTime(new String[]{".o", "o."});
		System.out.println("Time: " + (System.currentTimeMillis()-time)/1000.0 + " seconds");
		desiredAnswer = 1;
		System.out.println("Your answer:");
		System.out.println("\t" + answer);
		System.out.println("Desired answer:");
		System.out.println("\t" + desiredAnswer);
		if (answer != desiredAnswer)
		{
			errors = true;
			System.out.println("DOESN'T MATCH!!!!");
		}
		else
			System.out.println("Match :-)");
		System.out.println();
		time = System.currentTimeMillis();
		answer = new DucksAlignment().minimumTime(new String[]{".o...", "..o..", "....o"});
		System.out.println("Time: " + (System.currentTimeMillis()-time)/1000.0 + " seconds");
		desiredAnswer = 3;
		System.out.println("Your answer:");
		System.out.println("\t" + answer);
		System.out.println("Desired answer:");
		System.out.println("\t" + desiredAnswer);
		if (answer != desiredAnswer)
		{
			errors = true;
			System.out.println("DOESN'T MATCH!!!!");
		}
		else
			System.out.println("Match :-)");
		System.out.println();
		time = System.currentTimeMillis();
		answer = new DucksAlignment().minimumTime(new String[]{"o..........", "..o........", ".......o...", "...........", "...........", "...........", "........o..", "..........."});
		System.out.println("Time: " + (System.currentTimeMillis()-time)/1000.0 + " seconds");
		desiredAnswer = 16;
		System.out.println("Your answer:");
		System.out.println("\t" + answer);
		System.out.println("Desired answer:");
		System.out.println("\t" + desiredAnswer);
		if (answer != desiredAnswer)
		{
			errors = true;
			System.out.println("DOESN'T MATCH!!!!");
		}
		else
			System.out.println("Match :-)");
		System.out.println();
		time = System.currentTimeMillis();
		answer = new DucksAlignment().minimumTime(new String[]{".........", "....o....", "........."});
		System.out.println("Time: " + (System.currentTimeMillis()-time)/1000.0 + " seconds");
		desiredAnswer = 0;
		System.out.println("Your answer:");
		System.out.println("\t" + answer);
		System.out.println("Desired answer:");
		System.out.println("\t" + desiredAnswer);
		if (answer != desiredAnswer)
		{
			errors = true;
			System.out.println("DOESN'T MATCH!!!!");
		}
		else
			System.out.println("Match :-)");
		System.out.println();
		time = System.currentTimeMillis();
		answer = new DucksAlignment().minimumTime(new String[]{"...o..........................", "............................o.", ".o............................", "............o.................", ".................o............", "......................o.......", "......o.......................", "....o.........................", "...............o..............", ".......................o......", "...........................o..", ".......o......................"});
		System.out.println("Time: " + (System.currentTimeMillis()-time)/1000.0 + " seconds");
		desiredAnswer = 99;
		System.out.println("Your answer:");
		System.out.println("\t" + answer);
		System.out.println("Desired answer:");
		System.out.println("\t" + desiredAnswer);
		if (answer != desiredAnswer)
		{
			errors = true;
			System.out.println("DOESN'T MATCH!!!!");
		}
		else
			System.out.println("Match :-)");
		System.out.println();
		
		
		if (errors)
			System.out.println("Some of the test cases had errors :-(");
		else
			System.out.println("You're a stud (at least on the test data)! :-D ");
	}

}
// Powered by [KawigiEdit] 2.0!
